// 引入 sercice 
const {
    dologin,
    dologout,
    createuser,
    edituser,
    getuserlist,
    getuser,
    getuserbyname,
    remove,
    activation,
    editpass
} = require('../../service/user/user');

exports.userApi = async ctx => {
    // 获取url 字段 继而判断接下来操作
    let url = ctx.request.url.split('/');
    let urlKey = url[2].split('?')[0];
    const { id } = ctx.params || '';
    const { pageNum, pageSize } = ctx.query || '';
    const { username, password, newpassword, nickname, creator, role, role_id, sex, phone, mail, effect, status, avatar, updator } = ctx.request.body || '';
    console.log(username, password);
    let res;
    switch (ctx.request.method) {
        case 'GET':
            if (urlKey == 'byid') {
                res = await getuser({ id });
            } else if (urlKey == 'searchuser') {
                res = await getuserbyname({ nickname, username, pageNum, pageSize });
            } else if (urlKey == 'list') {
                res = await getuserlist({ pageNum, pageSize });
            }
            break;
        case 'POST':
            if (urlKey == 'login') {
                res = await dologin({ username, password });
            } else if (urlKey == 'activation') {
                res = await activation({ id, updator });
            } else if (urlKey == 'register') {
                res = await createuser({ nickname, username, password, creator, role_id, phone, mail, sex });
            }
            break;
        case 'PUT':
            if (urlKey == 'edit') {
                res = await edituser({ nickname, id, role_id, sex, phone, mail, effect, status, avatar, role });
            } else if (urlKey == 'editpass') {
                res = await editpass({ id, newpassword, password, username });
            }
            break;
        case 'DELETE':
            res = await remove({ id, updator });
            break;
        default:
            res = { msg: '操作失败', code: '400' };
    }
    ctx.body = res;
};